//
//  YLAESTool.m
//  Yilong
//
//  Created by DisCover on 2021/6/23.
//

#import "YLAESTool.h"
#import <CommonCrypto/CommonDigest.h>
#import <CommonCrypto/CommonCryptor.h>

@implementation YLAESTool

//解密

+ (NSData*)AES128Decrypt:(NSData*)data key:(Byte*)akey{
    unsigned char result[kCCKeySizeAES128];
    bzero(result, sizeof(result));
    size_t numBytesCrypted = 0;
    CCCryptorStatus crypyStatus = CCCrypt(kCCDecrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding|kCCOptionECBMode, akey, kCCKeySizeAES128, NULL, data.bytes, data.length, result, sizeof(result), &numBytesCrypted);
    if (crypyStatus == kCCSuccess) {
        NSData*resultData = [NSData dataWithBytes:result length:    numBytesCrypted];
        return  resultData;
    }
    return  nil;
}
//加密
+ (NSData*)AES128Encrypt:(NSData*)data key:(Byte*)aKey{
    unsigned char result[kCCKeySizeAES128];
    bzero(result, sizeof(result));
    size_t numBytesCrypted = 0;
    CCCryptorStatus crypyStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding|kCCOptionECBMode, aKey, kCCKeySizeAES128, NULL, data.bytes, data.length, result, sizeof(result), &numBytesCrypted);
    if (crypyStatus == kCCSuccess) {
        NSData*resultData = [NSData dataWithBytes:result length:    numBytesCrypted];
        return  resultData;
    }
    return  nil;

}
+(NSData*)AES_NEW_AESEnString:(NSData*)plain pass:(NSString*)key

{
    
    // NSLog(@"key是%@",key);
    
    char keyPtr[kCCKeySizeAES128+1];
    
    memset(keyPtr,0,sizeof(keyPtr));
    [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];

//    [key getBytes:keyPtr length:sizeof(keyPtr)];
    
    //NSLog(@"加密的KEY是%s",keyPtr);
    
    NSData* data = [[NSData alloc]initWithData:plain];
    
    NSUInteger dataLength = [data length];
    
    size_t bufferSize = dataLength + kCCBlockSizeAES128;
    
    void*buffer = malloc(bufferSize);
    
    size_t numBytesEncrypted =0;
    
    CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt,
                                          
                                          kCCAlgorithmAES128,
                                          
                                          kCCOptionECBMode,
                                          
                                          keyPtr,
                                          
                                          kCCBlockSizeAES128,
                                          
                                          NULL,
                                          
                                          [data bytes],
                                          
                                          dataLength,
                                          
                                          buffer,
                                          
                                          bufferSize,
                                          
                                          &numBytesEncrypted);
    
    if(cryptStatus ==kCCSuccess) {
        
        NSMutableData*resultData = [NSMutableData dataWithBytes:buffer length:numBytesEncrypted];
        
        return resultData;
        
    }
    
    free(buffer);
    
    return nil;
    
}

+(NSData*)AES_NEW_AESEn:(NSData*)plain pass:(NSData*)key

{
    
    // NSLog(@"key是%@",key);
    
    char keyPtr[kCCKeySizeAES128+1];
    
    memset(keyPtr,0,sizeof(keyPtr));
    
    [key getBytes:keyPtr length:sizeof(keyPtr)];
    
    //NSLog(@"加密的KEY是%s",keyPtr);
    
    NSData* data = [[NSData alloc]initWithData:plain];
    
    NSUInteger dataLength = [data length];
    
    size_t bufferSize = dataLength + kCCBlockSizeAES128;
    
    void*buffer = malloc(bufferSize);
    
    size_t numBytesEncrypted =0;
    
    CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt,
                                          
                                          kCCAlgorithmAES128,
                                          
                                          kCCOptionECBMode,
                                          
                                          keyPtr,
                                          
                                          kCCBlockSizeAES128,
                                          
                                          NULL,
                                          
                                          [data bytes],
                                          
                                          dataLength,
                                          
                                          buffer,
                                          
                                          bufferSize,
                                          
                                          &numBytesEncrypted);
    
    if(cryptStatus ==kCCSuccess) {
        
        NSMutableData*resultData = [NSMutableData dataWithBytes:buffer length:numBytesEncrypted];
        
        return resultData;
        
    }
    
    free(buffer);
    
    return nil;
    
}
+(NSData*)AES_NEW_AESDecrypt:(NSData*)encryptText password:(NSData*)key

{
    
    char keyPtr[kCCKeySizeAES128+1];
    
    memset(keyPtr,0,sizeof(keyPtr));
    
    [key getBytes:keyPtr length:sizeof(keyPtr)];
    
    NSData*data=[[NSData alloc]initWithData:encryptText];
    
    NSUInteger dataLength = [data length];
    
    size_t bufferSize = dataLength +kCCBlockSizeAES128;
    
    void*buffer =malloc(bufferSize);
    
    size_t numBytesCrypted =0;
    
    CCCryptorStatus cryptStatus =CCCrypt(kCCDecrypt,
                                         
                                         kCCAlgorithmAES128,
                                         
                                         kCCOptionECBMode,
                                         
                                         keyPtr,
                                         
                                         kCCBlockSizeAES128,
                                         
                                         NULL,
                                         
                                         [data bytes],
                                         
                                         dataLength,
                                         
                                         buffer,
                                         
                                         bufferSize,
                                         
                                         &numBytesCrypted);
    
    if(cryptStatus ==kCCSuccess) {
        
        NSMutableData*resultData = [NSMutableData dataWithBytesNoCopy:buffer length:numBytesCrypted];
        
        return resultData;
        
    }
    
    free(buffer);
    
    return nil;
    
}




@end
